/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.img;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

/**
 * <p>Project: app2022 - Img7
 * <p>Powered by webrx On 2022-01-21 17:37:28
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Img7 {
    public static void main(String[] args) {
        var w = 800;
        var h = 800;
        var t = 2;
        var i = new BufferedImage(w, h, t);
        var g = i.createGraphics();
        //设置线条和文件的，抗锯齿（平滑）
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);
        g.setColor(Color.ORANGE);
        g.fillRect(0, 0, w, h);

        g.setStroke(new BasicStroke(6));

        g.setColor(Color.RED);
        Random rand = new Random();
        int yy = rand.nextInt(15) + h / 2;// y坐标位置
        int hh = rand.nextInt(10, 20);// 高度
        int aa = rand.nextInt(60) + 20;//频率
        // x sin曲线
        for (int x = 10; x < w; x++) {
            int y = (int) (yy + hh * Math.sin(x * Math.PI / aa));
            g.drawLine(x, y, x, y);
            //g.fillOval(x, y, 5, 5);
        }


        hh = rand.nextInt(10, 50);// 宽度
        //y cos 曲线
        for (int y = 10; y < h; y++) {
            int x = (int) (yy + hh * Math.cos(y * Math.PI / aa));
            g.drawLine(x, y, x, y);
            //g.fillOval(x, y, 5, 5);
        }

        g.dispose();
        //保存图像文件
        try {
            ImageIO.write(i, "png", new File("i.png"));
        } catch (Exception e) {
            e.printStackTrace();
        }

        //打开图像文件
        try {
            Runtime.getRuntime().exec("cmd /k start i.png");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
